<?php

include('common.php');


// get the defaults
$default_row = sql_row1("SELECT Bonus_Field, Returns_To FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number='$line' ");
$constant_default = $default_row['Bonus_Field'];
$variable_default = $default_row['Returns_To'];

function variables_dropdown($domain, $default)
{
	$user = user();
	
	$safe_sql = "SELECT ID, Name FROM Variables WHERE (ID > -1 AND Domain='$domain' AND User='$user') AND Name IS NOT NULL ORDER BY ID DESC";
	$result = sql_exec($safe_sql);
	while ($row = mysql_fetch_array($result))
	{
		$id = $row['ID'];
		// for it\'s a variable
		$name = stripslashes($row['Name']);
		$selected = ($default == $id) ? 'selected' : '';
		$menu .= "<option $selected value=\"$id\">$name</option>\n";
	}
	if (!empty($menu))
	{
		$menu = "<select name=\"variable\">\n" . $menu . "</select>\n";
	} else {
		$menu = "<p>you need to <a href=\"memory.php?domain=$domain\">add a variable</a></p>";
	}
	return $menu;
}

// catch form submission and redirect
if (!empty($G['submit']) || !empty($G['constant']))
{
	$variable = $G['variable'];
	$constant = $G['constant'];
	sql_exec("UPDATE Instances SET Returns_To='$variable', Bonus_Field='$constant' WHERE User='$user' AND Domain='$domain' AND Line_Number='$line' ");
	update($user, $domain, $line);
	header("Location: edit_page.php?domain=$domain");
}

html_header("store a constant");
?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<h4>store a constant in a variable</h4>

<form action="store_constant.php" method="get">

constant: <input name="constant" value="<?php echo $constant_default; ?>">

<p>stored in: <?php echo variables_dropdown($domain, $variable_default); ?></p>

<input type="hidden" name="domain" value="<?php echo $domain; ?>" />
<input type="hidden" name="line" value="<?php echo $line; ?>" />

<input type="submit" name="submit" value="store" />
</form>

<?php echo "<p><a href=\"delete_instance.php?line=$line&domain=$domain\">delete</a></p>"; ?>

<?php echo footer(); ?>



</div>

</div>
</body>
</html>
